﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiseLinkWPF.Windows.Controls.GridView;
using CCF20Model;
using System.Collections;
using WiseLinkWPF.Windows.Controls;
namespace CCF20
{
    /// <summary>
    /// Interaction logic for DeviceSale.xaml
    /// </summary>
    public partial class Customer : UserControl
    {
        private CustomerModel model;
        private CollectionViewSource VehicleTypeLookupView;

        public Customer()
        {
            
            InitializeComponent();
            model = new CustomerModel();
            DataContext = model.GetCustomer();
        }
    
        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            this.radGridView1.Items.CommitEdit();
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            this.radGridView1.Items.EditItem(this.radGridView1.CurrentItem);
        }
        private void Customer_Click(object sender, RoutedEventArgs e)
        {
            CCFZIC ccfZIC = new CCFZIC();
            string strAction = ((Button)sender).Name.ToString();
            switch (strAction)
            {
                case "Customer_Transfer":

                    CustomerData tCustomer = radGridView1.CurrentItem as CustomerData;
                    if (tCustomer.MyCustomer.IsTransfered == true)
                    {
                        ccfZIC.ConfirmTransferCustomer();
                    }

                    break;
                
                case "Customer_Print":

                    CustomerData pCustomer = radGridView1.CurrentItem as CustomerData;
                    PrintCustomer printCustomer = new PrintCustomer(pCustomer);
                    printCustomer.Show();
                    break;
                
                case "Customer_Add":
                    
                    
                    ((IList)this.radGridView1.ItemsSource).Add(new CustomerData());
                    radGridView1.Items.MoveToLastPage();
                    radGridView1.Items.MoveCurrentToLast();
                    break;
                case "Customer_Remove":
                    int selectionCount = radGridView1.SelectedItems.Count;
                    if (selectionCount == 0)
                    {
                        MessageBox.Show("请先选中表格中的数据", "CCF2.0", MessageBoxButton.OK);
                        return;
                    }
                    CustomerData removeCustomer = radGridView1.SelectedItem as CustomerData;
                    if (removeCustomer.MyCustomer.Devices.Count > 0)
                    {
                        MessageBox.Show(string.Format("该客户仍然拥有售出的设备, IDC号 {0}，不能删除",removeCustomer.MyDevice.DeviceKey)
                                    , "CCF2.0", MessageBoxButton.OK);
                        return;
                    }

                    MessageBoxResult confirmResult = MessageBox.Show(string.Format("请确认删除表格中的{0}条数据",selectionCount), "CCF2.0", MessageBoxButton.OKCancel);

                    if (confirmResult != MessageBoxResult.OK)
                    {
                        return;
                    }

                    ((IList)this.radGridView1.ItemsSource).Remove(this.radGridView1.SelectedItem);
                    radGridView1.Items.Refresh();
                    model.Save(null);
                    break;
                case "Customer_Save":
                    

                    CustomerData currentCustomer = (CustomerData)this.radGridView1.CurrentItem;
                    //when there is no data in the grid
                    if (currentCustomer == null)
                        return;
                    Window parentWindow = Window.GetWindow(this);
                    foreach (TextBox tb in Help.FindVisualChildren<TextBox>(parentWindow)) 
                    {
                        // do something with tb here 
                        var f = tb.GetBindingExpression(TextBox.TextProperty);
                        if (f != null)
                            f.UpdateSource();

                    }
                    foreach (RadDatePicker dp in Help.FindVisualChildren<RadDatePicker>(parentWindow)) 
                    {
                        // do something with tb here 
                        var f = dp.GetBindingExpression(RadDatePicker.SelectedDateProperty);
                        if (f != null)
                            f.UpdateSource();

                    }

                    foreach (RadComboBox cb in Help.FindVisualChildren<RadComboBox>(parentWindow))
                    {
                        // do something with tb here 
                        var f = cb.GetBindingExpression(RadComboBox.SelectedItemProperty);
                        
                        if (f != null)
                            f.UpdateSource();

                    }
                    

                    if ((currentCustomer.MyCustomer.Error != null)||
                        (currentCustomer.MyTransfered.Error != null) ||
                        (currentCustomer.MyVehicle.Error != null))
                    {
                        MessageBox.Show("客户数据有错，请先修改", "CCF2.0", MessageBoxButton.OK);
                        return;
                    }
                    else if (currentCustomer.MyCustomer.Gender == null)
                    {
                        MessageBox.Show("客户性别不可为空", "CCF2.0", MessageBoxButton.OK);
                        return;
                    }
                    else if (currentCustomer.MyVehicle.VehicleType == null)
                    {
                        MessageBox.Show("车辆型号不可为空", "CCF2.0", MessageBoxButton.OK);
                        return;
                    }
                    else if (currentCustomer.MyCustomer.IsTransfered == true)
                    {
                        if (string.IsNullOrEmpty(currentCustomer.MyTransfered.DeviceKey) ||
                            string.IsNullOrEmpty(currentCustomer.MyTransfered.IDCNum))
                        {
                            MessageBox.Show("转移客户的设备序列号和IDC号不可为空", "CCF2.0", MessageBoxButton.OK);
                            return;
                        }
                        
                    }
                    TextBox  currentFocusdTextBox = Keyboard.FocusedElement as TextBox;
                    if (currentFocusdTextBox != null)
                    {
                        currentFocusdTextBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                    }
                    currentCustomer.MyCustomer.Operator = (string)Application.Current.Properties["CurrentUser"];
                    model.Save(currentCustomer);
                    MessageBox.Show("保存成功","CCF2.0", MessageBoxButton.OK);

                    if (currentCustomer.MyDevice != null && currentCustomer.MyDevice.IsTransfer == true)
                    {
                        //该转移客户已经确认过了
                        return;
                    }else if (currentCustomer.MyCustomer.IsTransfered == true) 
                    {
                        if (ccfZIC.TransferCustomer(currentCustomer.MyTransfered))
                        {
                            MessageBox.Show("上传转移客户成功", "CCF2.0", MessageBoxButton.OK);
                        }
                    }

                    
                    break;
            }
        }
            
        private void radGridView1_SelectionChanged(object sender, WiseLinkWPF.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (e.AddedItems.FirstOrDefault() != null)
            {
                this.radGridView1.CurrentItem = e.AddedItems.First();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            VehicleTypeLookupView = (CollectionViewSource)this.FindResource("VehicleTypeLookup");
            model.GetMyVehicleTypes();
            VehicleTypeLookupView.Source = model.MyVehicleTypes;
            
        }

        private void StackPanel_Error(object sender, ValidationErrorEventArgs e)
        {
            MessageBox.Show(e.Error.Exception.Message);
        }
        
        
    }
}
